/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.global;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.devoxy.roboeye.R;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 03.07.11
 * Time: 22:13
 */
public class PreferencesHelper {

    private static final Context context;
    private static final SharedPreferences settings;

    static {
        context = ContextHolder.getInstance().getContext();
        settings = context.getSharedPreferences(context.getString(R.string.shared_prefs_file), Context.MODE_PRIVATE);
    }

    public static void initPreferences() {
        // setup preferences from settings screen
        PreferenceManager.setDefaultValues(context, context.getString(R.string.shared_prefs_file), Context.MODE_PRIVATE, R.xml.preferences, false);

        // setup other preferences
        SharedPreferences.Editor editor = settings.edit();
        if (!settings.contains(getPrefKey(R.string.is_license_shown_pref))) {
            editor.putBoolean(getPrefKey(R.string.is_license_shown_pref), getDefaultBool(R.bool.is_license_shown_default));
        }
        editor.putBoolean(getPrefKey(R.string.is_splash_shown_pref), getDefaultBool(R.bool.is_splash_shown_default));
        editor.commit();
    }

    @SuppressWarnings("unchecked")
    public static <T> T getPref(int prefKeyId) {
        switch (prefKeyId) {
            case R.string.vibration_pref:
                return (T) getBoolean(prefKeyId, R.bool.vibration_default);
            case R.string.notification_pref:
                return (T) getBoolean(prefKeyId, R.bool.notification_default);
            case R.string.autoname_pref:
                return (T) getBoolean(prefKeyId, R.bool.autoname_default);
            case R.string.rotate_pref:
                return (T) getString(prefKeyId, R.string.rotate_default);
            case R.string.save_to_pref:
                return (T) getString(prefKeyId, R.string.save_to_default);
            case R.string.is_splash_shown_pref:
                return (T) getBoolean(prefKeyId, R.bool.is_splash_shown_default);
            case R.string.is_license_shown_pref:
                return (T) getBoolean(prefKeyId, R.bool.is_license_shown_default);
            case R.string.is_app_crashed_pref:
                return (T) getBoolean(prefKeyId, R.bool.is_app_crashed_default);
            case R.string.crash_reports_pref:
                return (T) getBoolean(prefKeyId, R.bool.crash_reports_default);
            case R.string.language_pref:
                return (T) getString(prefKeyId, R.string.language_default);
            default:
                throw new IllegalArgumentException("Unknown preference = " + getPrefKey(prefKeyId));
        }
    }

    public static void setPref(int prefKeyId, Object value) {
        SharedPreferences.Editor editor = settings.edit();
        if (value instanceof Boolean) {
            editor.putBoolean(getPrefKey(prefKeyId), (Boolean) value);
        } else if (value instanceof String) {
            editor.putString(getPrefKey(prefKeyId), (String) value);
        } else if (value instanceof Float) {
            editor.putFloat(getPrefKey(prefKeyId), (Float) value);
        } else if (value instanceof Integer) {
            editor.putInt(getPrefKey(prefKeyId), (Integer) value);
        } else if (value instanceof Long) {
            editor.putLong(getPrefKey(prefKeyId), (Long) value);
        } else {
            throw new IllegalArgumentException("Unsupported preference type. pref = " + getPrefKey(prefKeyId) + " value = " + value);
        }
        editor.commit();
    }

    public static String pathToPhotos() {
        return PreferencesHelper.getPref(R.string.save_to_pref) + context.getString(R.string.photos_dir);
    }

    public static String pathToResults() {
        return PreferencesHelper.getPref(R.string.save_to_pref) + context.getString(R.string.results_dir);
    }

    public static boolean isDebug() {
        return context.getResources().getBoolean(R.bool.debug);
    }

    private static String getString(int prefKeyId, int defaultValueId) {
        return settings.getString(getPrefKey(prefKeyId), getDefaultString(defaultValueId));
    }

    private static Boolean getBoolean(int prefKeyId, int defaultValueId) {
        return settings.getBoolean(getPrefKey(prefKeyId), getDefaultBool(defaultValueId));
    }

    private static String getPrefKey(int prefKeyId) {
        return context.getString(prefKeyId);
    }

    private static boolean getDefaultBool(int boolId) {
        return context.getResources().getBoolean(boolId);
    }

    private static String getDefaultString(int stringId) {
        return context.getString(stringId);
    }

}
